function [] = count_bits(main_matrix, streams, component_num, file)
    entropyStr = {'Enpropy:'};
    headers = {'Threshold', 'matrix_idx', 'result (bits count)'};
    golomb_headers = {'Golomb K', 'matrix_idx', 'Threshold', 'result (bits count)'};
   
    %write_main_matrix_results(main_matrix, component_num, file);
    
    l = length(streams);
    stream_sheet = file;
	for i = 4:6
        maxi = get_max_for_level(streams, i) + 1;
        %stream_sheet = file;
        file = ['stream(' ' ' num2str(component_num) ' ' ' , ' ' ' num2str(i) ' ' ')'];
       
        xlswrite(file, {'Entropy'}, stream_sheet, 'A2');
        xlswrite(file, {'Matrix idx', 'Entropy'}, stream_sheet, 'A3');
        xlswrite(file, count_entropy_for_streams(streams, i), stream_sheet, 'A4');
    
       xlswrite(file, {'Krichevskiy without preprocessing:'}, stream_sheet, 'D3');
       xlswrite(file, count_bits_for_level(streams, i, 0, 'K', 1, 0), stream_sheet, 'D4');
    
       xlswrite(file, {'Krichevskiy with logarithm preprocessing'}, stream_sheet, 'H2');
       xlswrite(file, headers, stream_sheet, 'H3');
       xlswrite(file, count_bits_for_level(streams, i, 1, 'K', maxi, 0), stream_sheet, 'H4');
    
       xlswrite(file, {'Krichevskiy with escape-code preprocessing'}, stream_sheet, 'M2');
       xlswrite(file, headers, stream_sheet, 'M3');
       xlswrite(file, count_bits_for_level(streams, i, 2, 'K', maxi, 0), stream_sheet, 'M4');
       
       
       xlswrite(file, {'Algorithm A without preprocessing:'}, stream_sheet, 'R3');
       xlswrite(file, count_bits_for_level(streams, i, 0, 'A', 1, 0), stream_sheet, 'R4');
    
       xlswrite(file, {'Algorithm A with logarithm preprocessing'}, stream_sheet, 'X2');
       xlswrite(file, headers, stream_sheet, 'X3');
       xlswrite(file, count_bits_for_level(streams, i, 1, 'A', maxi, 0), stream_sheet, 'X4');
    
       xlswrite(file, {'Algorithm A with escape-code preprocessing'}, stream_sheet, 'AD2');
       xlswrite(file, headers, stream_sheet, 'AD3');
       xlswrite(file, count_bits_for_level(streams, i, 2, 'A', maxi, 0), stream_sheet, 'AD4');
       
       
       xlswrite(file, {'Levenstein without preprocessing:'}, stream_sheet, 'AI3');
       xlswrite(file, count_bits_for_level(streams, i, 0, 'L', 1, 0), stream_sheet, 'AI4');
    
       xlswrite(file, {'Levenstein with logarithm preprocessing'}, stream_sheet, 'AM2');
       xlswrite(file, headers, stream_sheet, 'AM3');
       xlswrite(file, count_bits_for_level(streams, i, 1, 'L', maxi, 0), stream_sheet, 'AM4');
    
       xlswrite(file, {'Levenstein with escape-code preprocessing'}, stream_sheet, 'AR2');
       xlswrite(file, headers, stream_sheet, 'AR3');
       xlswrite(file, count_bits_for_level(streams, i, 2, 'L', maxi, 0), stream_sheet, 'AR4');
       
       
       xlswrite(file, {'Golomb with logarithm preprocessing'}, stream_sheet, 'AW2');
       xlswrite(file, golomb_headers, stream_sheet, 'AW3');
       xlswrite(file, count_bits_for_level_Golomb(streams, i, 1, maxi), stream_sheet, 'AW4');
    
       xlswrite(file, {'Golomb with escape-code preprocessing'}, stream_sheet, 'BC2');
       xlswrite(file, golomb_headers, stream_sheet, 'BC3');
       xlswrite(file, count_bits_for_level_Golomb(streams, i, 2, maxi), stream_sheet, 'BC4');
       
       xlswrite(file, {'Krichevskiy for signs without preprocessing:'}, stream_sheet, 'BI3');
       xlswrite(file, count_bits_for_level(streams, i, 0, 'K', 1, 1), stream_sheet, 'BI4');

       xlswrite(file, {'Algorithm A for signs without preprocessing:'}, stream_sheet, 'BN3');
       xlswrite(file, count_bits_for_level(streams, i, 0, 'A', 1, 1), stream_sheet, 'BN4');
       
       xlswrite(file, {'Count signs'}, stream_sheet, 'BR3');
       xlswrite(file, count_bits_for_level(streams, i, 0, 'A', 1, 2), stream_sheet, 'BR4');

	end;
end

function [] = write_main_matrix_results(main_matrix, component_num, file)
    entropyStr = {'Enpropy:'};
    headers = {'Threshold', 'result (bits count)'};
    golomb_headers = {'Golomb K', 'Threshold', 'result (bits count)'};
	
    main_matrix_sheet = ['Main matrix' ' ' num2str(component_num)];
    
	bits = count_entropy_for_main_matrix(main_matrix);
    xlswrite(file, entropyStr, main_matrix_sheet, 'A1');
    xlswrite(file, bits, main_matrix_sheet, 'B1');
    
    xlswrite(file, {'Krichevskiy without preprocessing:'}, main_matrix_sheet, 'A3');
    xlswrite(file, adaptator(main_matrix, 1, 0, 0, 'K', 0, 0), main_matrix_sheet, 'A3');
    
    xlswrite(file, {'Krichevskiy with logarithm preprocessing'}, main_matrix_sheet, 'A5');
    xlswrite(file, headers, main_matrix_sheet, 'A6');
    xlswrite(file, count_bits_for_matrix(main_matrix, 1, 'K'), main_matrix_sheet, 'A7');
    
    xlswrite(file, {'Krichevskiy with escape-code preprocessing'}, main_matrix_sheet, 'D5');
    xlswrite(file, headers, main_matrix_sheet, 'D6');
    xlswrite(file, count_bits_for_matrix(main_matrix, 2, 'K'), main_matrix_sheet, 'D7');
    
    
    xlswrite(file, {'Algorithm A without preprocessing:'}, main_matrix_sheet, 'G3');
    xlswrite(file, adaptator(main_matrix, 1, 0, 0, 'A', 0, 0), main_matrix_sheet, 'H3');
    
    xlswrite(file, {'Algorithm A with logarithm preprocessing'}, main_matrix_sheet, 'G5');
    xlswrite(file, headers, main_matrix_sheet, 'G6');
    xlswrite(file, count_bits_for_matrix(main_matrix, 1, 'A'), main_matrix_sheet, 'G7');
    
    xlswrite(file, {'Algorithm A with escape-code preprocessing'}, main_matrix_sheet, 'J5');
    xlswrite(file, headers, main_matrix_sheet, 'J6');
    xlswrite(file, count_bits_for_matrix(main_matrix, 2, 'A'), main_matrix_sheet, 'J7');

    
    xlswrite(file, {'Levenstein without preprocessing:'}, main_matrix_sheet, 'M3');
    xlswrite(file, adaptator(main_matrix, 1, 0, 0, 'L', 0, 0), main_matrix_sheet, 'N3');
    
    xlswrite(file, {'Lenvenstein with logarithm preprocessing'}, main_matrix_sheet, 'M5');
    xlswrite(file, headers, main_matrix_sheet, 'M6');
    xlswrite(file, count_bits_for_matrix(main_matrix, 1, 'L'), main_matrix_sheet, 'M7');
    
    xlswrite(file, {'Lenvenstein with escape-code preprocessing'}, main_matrix_sheet, 'P5');
    xlswrite(file, headers, main_matrix_sheet, 'P6');
    xlswrite(file, count_bits_for_matrix(main_matrix, 2, 'L'), main_matrix_sheet, 'P7');
    
    
    xlswrite(file, {'Golomb with logarithm preprocessing'}, main_matrix_sheet, 'S5');
    xlswrite(file, golomb_headers, main_matrix_sheet, 'S6');
    xlswrite(file, count_bits_for_matrix_G(main_matrix, 1), main_matrix_sheet, 'S7');
    
    xlswrite(file, {'Golomb with escape-code preprocessing'}, main_matrix_sheet, 'W5');
    xlswrite(file, golomb_headers, main_matrix_sheet, 'W6');
    xlswrite(file, count_bits_for_matrix_G(main_matrix, 2), main_matrix_sheet, 'W7');

end

function [output] = count_bits_for_level(level_stream, idx, preprocessing, flag, max_threshold, for_signs)
    if (flag == 'G')
        output = count_bits_for_level_Golomb(level_stream, idx, preprocessing, max_threshold);
        return;
    end;

    if (preprocessing == 0)
        max_threshold = 1;
    end;
    output = zeros(max_threshold * 3, 3);
    
    output_idx = 1;
    for threshold = 1:max_threshold
        for j = 1:3
            matrix_stream = level_stream{idx, j};
            l = length(matrix_stream);
            bits = 0;
            for k = 1:l
                bits = bits + adaptator(matrix_stream{k}, 0, preprocessing, threshold, flag, 0, for_signs);
            end;
            output(output_idx, :) = [threshold, j, bits];
            output_idx = output_idx + 1;
        end;
        
    end;
    
    if (preprocessing == 0)
        output = output(:, 2:3);
    end;
end

function [output] = count_bits_for_level_Golomb(level_stream, idx, preprocessing, max_threshold)
    output = zeros(max_threshold * 8 * 3, 4);
    
    output_idx = 1;
    for threshold_G = 1:8
        for threshold = 1:max_threshold
            for j = 1:3
                matrix_stream = level_stream{idx, j};
                l = length(matrix_stream);
                bits = 0;
                for k = 1:l
                    bits = bits + adaptator(matrix_stream{k}, 0, preprocessing, threshold, 'G', threshold_G, 0);
                end;
                output(output_idx, :) = [j, threshold_G, threshold, bits];
                output_idx = output_idx + 1;
            end;
        end;
    end;
end

function [output] = count_bits_for_matrix(matrix, preprocessing, flag)
    maxi = max(max(matrix)) + 1;
    output = zeros(maxi, 2);
    for j = 1:maxi
        res = adaptator(matrix, 1, preprocessing, j, flag, 0, 0);
        output(j, :) = [j, res];
    end;
end

function [output] = count_bits_for_matrix_G(matrix, preprocessing)
    maxi = max(max(matrix)) + 1;
    output = zeros(maxi * 8, 3);
    
    idx = 1;
    for i = 1:8
        for j = 1:maxi
            res = adaptator(matrix, 1, preprocessing, j, 'G', i, 0);
            output(idx, :) = [i, j, res];
            idx = idx + 1;
        end;
    end;
end

function [bits] = count_entropy_for_streams(streams, idx)
    bits = zeros(3, 2);
	for j = 1:3
		matrix_stream = streams{idx, j};
        b = 0;
		for k = 1:length(matrix_stream)
			b = b + entropm(matrix_stream{k});
		end;
        bits(j, :) = [j, b];
	end;
end

function [bits] = count_entropy_for_main_matrix(matrix)
    bits = 0;
	if (sum(matrix >= 0) == prod(matrix))
        bits = bits + entropm(diff_cod(matrix));
    else
        bits = bits + entropm(matrix);
    end;
end

function [maxi] = get_max_for_level(level_stream, idx)
    arr = zeros(1, 3 * length(level_stream{idx, 1}));
    for j = 1:3
        matrix_stream = level_stream{idx, j};
        for k = 1:length(matrix_stream)    
            arr(j * k) = max(matrix_stream{k});
        end;
    end;
    if (isempty(arr))
        maxi = 0;
    else
        maxi = max(arr);
    end
end
